package com.gdit.zax.thread;

import java.io.File;
import java.sql.Time;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.gdit.zax.utils.LogUtil;
import com.jcloud.jss.JingdongStorageService;
import com.jcloud.jss.domain.Bucket;
import com.jcloud.jss.domain.ObjectListing;
import com.jcloud.jss.domain.ObjectSummary;



public class LoopFileThread extends Thread {
	private String webdir;
	
	public LoopFileThread (String webpath) {
		webdir = webpath;
	}
	
	@Override
	public void run() {
		LogUtil.print("开始检测文件...");
		
		JingdongStorageService jss = new JingdongStorageService("864c1e49e22c46a9b15416348240acc8","86d76763b51b435fbe4425dbd53cf0efDDFXcPZu");
		
		//建立目录>>远程目录
		File webpath0 = new File(webdir);
		for (File subdir:webpath0.listFiles()) {
			if (subdir.isDirectory()) {
				LogUtil.print("创建远程目录:" + subdir.getName());
				try {	
					jss.bucket("webapp0" +subdir.getName()).create();
				} catch (Exception e) {
					LogUtil.print("创建远程目录失败" + e.getMessage());
				}
			}
		}

//		//创建远程目录>>目录
//		for (Bucket bck:jss.listBucket()) {
//			LogUtil.print("远程目录:" + bck.getName());
//			File subdir = new File(webdir + File.separator +  bck.getName().replace("webapp0", ""));
//			if (!subdir.exists()) {
//				LogUtil.print("创建本地目录:" + subdir.getAbsolutePath());
//				subdir.mkdirs();
//			}
//		}
		
		//远程文件>>本地文件
		for (Bucket bck:jss.listBucket()) {
			ObjectListing  filsse = jss.bucket(bck.getName()).listObject();
			for (ObjectSummary os : filsse.getObjectSummaries()) {
				String filepath = webdir + File.separator +  bck.getName().replace("webapp0", "");
				File filedir = new File(filepath);
				if (!filedir.exists()) {
					LogUtil.print("创建本地目录:" + filepath);
					filedir.mkdirs();
				}
				File tagfile = new File(filepath + File.separator + os.getKey());
				if (!tagfile.exists()) {
					try {
						LogUtil.print("下载远程文件:" + bck.getName() + ">>" + os.getKey() + ">>" +tagfile.getAbsolutePath());
						jss.bucket(bck.getName()).object(os.getKey()).get().toFile(tagfile);
					} catch (Exception e) {
						LogUtil.print("下载远程文件失败:" + e.getMessage());
					}
				}
//				os.getLastModified()//更新时间
			}
			
//			
//			File subdir = new File(webdir + bck.getName());
//			if (!subdir.exists()) {
//				LogUtil.print("创建本地目录:" + subdir.getAbsolutePath());
//				subdir.mkdirs();
//			}
		}
		
		
		
		while (true) {
			try {
				//1小时候上传一次
				sleep(1000*60*60);
			} catch (Exception e) {
			}
			
			
			//获取远程文件的时间
			Map<String, Long> map = new HashMap<String, Long>();
			
			for (Bucket bck:jss.listBucket()) {//列出远程文件夹
				ObjectListing  filsse = jss.bucket(bck.getName()).listObject();
				for (ObjectSummary os : filsse.getObjectSummaries()) {//列出远程文件夹文件
					long webmodefied = Date.parse(os.getLastModified());
					map.put(bck.getName().replace("webapp0", "") + "/" + os.getKey(), webmodefied);
				}
			}
			
			//本地文件>>远程文件
			for (File subdir:webpath0.listFiles()) {//列出本地文件夹
				if (subdir.isDirectory()) {
					for (File subdir2:subdir.listFiles()) {//列出本地文件
						if (subdir2.isFile()) {
							long filemodefied = subdir2.lastModified();
							Long webmodefied = map.get( subdir.getName() + "/" + subdir2.getName());
							if ((subdir2.length()/1048576.0) > 50) {//(小于仅上传50mb)
								LogUtil.print("文件不上传(大于50M):" + subdir2.getAbsolutePath());
							} else if (webmodefied == null || filemodefied > webmodefied) {
								LogUtil.print("上传新文件:" + subdir2.getAbsolutePath() + ">>" + subdir.getName() + ">>" +subdir2.getName());
								try {
									jss.bucket("webapp0" + subdir.getName()).object(subdir2.getName()).entity(subdir2).put();									
								} catch (Exception e) {
									LogUtil.print("上传文件失败:" + e.getMessage());
								}
							} else {
							}
						}
					}
				}
			}

		}
		


	}
	
}
